package com.voice.call.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.utils.IpUtil;
import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.service.SmsLogService;
import com.voice.call.utils.JSONUtil;
import com.voice.call.utils.StringUtil;
import com.voice.call.version.ApiVersion;

//短信管理
@RestController
@RequestMapping("/api/{version}/sms/")
public class SMSController {
	
	private final Logger log = LoggerFactory.getLogger(SMSController.class);
	
	@Resource
	private SmsLogService smsLogService;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "receive", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public JSONObject smsReceived(HttpServletRequest request,@RequestBody String requestStr) {
		//log.error("短信回调:{}",requestStr);
		//return new Result();
		
		
		JSONObject object = new JSONObject();
		object.put("code", 0);
		object.put("msg", "成功");
		return object;
		//return "{\"code\":0,\"msg\":\"成功\"}";
	}

	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "send", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result smsSend(HttpServletRequest request,@RequestBody Map<String, Object> param) {
		//log.debug("参数:{}",JSONUtil.objectToJson(param));
		String lang = ConstLang.getLang(param);
		if(param==null || StringUtil.isEmpty((String)param.get("deviceNo")) 
				|| StringUtil.isEmpty((String)param.get("phone")) || StringUtil.isEmpty((String)param.get("type"))){
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		String ip =IpUtil.getIpAddr(request);
		String smsType = param.get("type").toString();
		String v = "";
		if (param.get("v")!=null) {
			v = param.get("v").toString().trim();
		}
		
		Result rs = smsLogService.sendMsg(v,ip, (String)param.get("deviceNo"), (String)param.get("phone"),smsType);
		rs.changeLange(lang);
		return rs;
	}
}
